<?php

namespace Home\Controller;
use Home\Org\Util\Autoreportsqlserver;
use Home\Org\Util\OmsDatabase;

use Think\Model;

require './vendor/autoload.php';

class OmsReportController{

    protected $allSKU = "";

    public function __construct(){
        ini_set('memory_limit', '512M');
    }

    // 测试输出
    function index(){
        echo __FUNCTION__;
        exit;
    }

    /**
     * 产品运营分析:积分报表:生成主体数据
     */
    function report1_1(){

        ini_set('memory_limit', '512M');

        $this->report1_1TRUNCATET();

        $currDate = date('Y-m-d');
        $firstDay = date('Y-m-1');
        $howmanyDays = date("d");
        $before_7 = date("Y-m-d", strtotime("-7 day"));

        $lastMonthFirstDay = date("Y-m-1", strtotime("-1 month"));
        $currMonthFristDay = date('Y-m-1');
        $nextMonthFirstDay = date("Y-m-1", strtotime("+1 month"));
        $lastYearFirstDay = date("Y-1-1", strtotime("-1 year"));
        $lastYearEndDay = date("Y-12-31", strtotime("-1 year"));
        $currYearFirstDay = date("Y-1-1");
        $currYearEndDay = date("Y-12-31");

        import("@.ORG.Util.OmsDatabase");
        $omsDatabase = OmsDatabase::getInstance();
        $model = $omsDatabase->getModel();

        $filed = ("
       oms_order_detail.channel_code AS p_code,
       oms_channel.channel_name AS p_name,
       oms_order_detail.sku_code AS sku,
       oms_order_detail.product_name AS sku_name,
       sys_user.account AS product_manager,
       oms_product_spu.brand_name,
       ROUND(sum(oms_order_detail.num)/".$howmanyDays.") AS 'daily_apply'
        ");

        //这个join造成数据重复!
        //$join = "left join oms_warehouse_sku_stock as b on oms_order_detail.sku_code = b.sku_code left join oms_channel on oms_order_detail.channel_code = oms_channel.channel_code";
        //fix:
        $join = "left join oms_channel on oms_order_detail.channel_code = oms_channel.channel_code";
        $join .= " LEFT JOIN sys_user on oms_order_detail.product_manager = sys_user.id";
        $join .= " LEFT JOIN oms_product_sku on oms_order_detail.sku_code = oms_product_sku.sku_code";
        $join .= " LEFT JOIN oms_product_spu on oms_product_sku.spu_code = oms_product_spu.spu_code";

        $where="
        1=1
  and oms_order_detail.order_status in (230,999)
  and oms_order_detail.sku_code != ''
  and oms_order_detail.create_time >= '".$firstDay."' and oms_order_detail.create_time <= '".$currDate." '
        ";

        $group = "oms_order_detail.channel_code, oms_order_detail.sku_code";

        $order = "oms_order_detail.channel_code desc";

        //Datagrip:2
        $res = $model->db(4)->table('oms_order_detail')->join($join)->where($where)->group($group)->order($order)->field($filed)->select();
        //->limit(10)

        // save to database:

        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'p_code' => $item['p_code'],
                'p_name' => $item['p_name'],
                'sku' => $item['sku'],
                'sku_name' => $item['sku_name'],
                'product_manager' => $item['product_manager'],
                'brand_name' => $item['brand_name'],
                'daily_apply' => $item['daily_apply']
            );
            $r = $omsReport1M->add($saveData);
        }


        //上月兑换量
        $where = "
        1=1
  and order_status in (230,999)
  and create_time >= '".$lastMonthFirstDay."' and oms_order_detail.create_time < '".$currMonthFristDay."'
        ";
        $group = "channel_code, sku_code";
        $order = "sku_code";
        $filed = "
        channel_code AS p_code,
    sku_code AS sku,
    sum(num) AS lastmonth_apply
        ";
        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        //Datagrip:3

        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'lastmonth_apply' => $item['lastmonth_apply'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }

        //今年累计兑换量
        $where = "
        1=1
  and order_status in (230,999)
  and create_time >= '".$currYearFirstDay."' and oms_order_detail.create_time < '".$currYearEndDay."'
        ";
        $filed = "
        channel_code AS p_code,
    sku_code AS sku,
    sum(num) AS thisyear_apply
        ";
        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'thisyear_apply' => $item['thisyear_apply'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }

        //去年累计兑换量
        /*
        $where = "
        1=1
  and order_type = 1
  and order_status in (220, 0)
  and create_time >= '".$lastYearFirstDay."' and oms_order_detail.create_time < '".$lastYearEndDay."'
        ";
        $filed = "
        channel_code AS p_code,
    sku_code AS sku,
    sum(num) AS lastyear_apply
        ";
        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'lastyear_apply' => $item['lastyear_apply'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }
        */

        //每天兑换量
        $where = "
        1=1
  and order_status in (230,999)
  and sku_code != ''
  and create_time >= '".$currMonthFristDay."' and create_time < '".$nextMonthFirstDay."'
        ";
        $group = "channel_code, sku_code,DATE_FORMAT(create_time,'%Y-%m-%d')";
        $order = "sku_code";
        $filed = "
        channel_code AS p_code,
    sku_code AS sku,
    DATE_FORMAT(create_time,'day_%d') AS t_day,
    sum(num) AS t_num
        ";
        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        //Datagrip:4

        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                $item['t_day'] => $item['t_num'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }

        //可兑换天数=可用库存/近7日平均兑换量
        //近7日平均兑换量
        /*
        $where = "
        1=1
  and order_type = 1
  and order_status in (220, 0)
  and create_time >= '".$before_7."' and oms_order_detail.create_time < '".$currDate."'
        ";
        $group = "channel_code, sku_code";
        $order = "sku_code";
        $filed = "
        channel_code AS p_code,
    sku_code AS sku,
    ceiling(sum(num)/7) AS avg7apply
        ";
        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        //Datagrip:3

        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'available_day' => $item['avg7apply'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }
        echo "1";

        //可兑换天数
        $this->report1_1CalculateAvailableDay();
        echo "2";
        */

        //去年累计
        $this->report1_1CalculateLastyearApply();
        echo "3";

        //今年累计SAP
        $this->report1_1CalculateThisyearApplySAP();
        echo "4";
        //今年累计
        $this->report1_1CalculateThisyearApply();
        echo "5";

        //SAP在途
        //$this->report1_1CalculateOnHand();
        //OMS可用库存
        $this->report1_1CalculateOnHandOMS();
        echo "6";

        //echo json_encode($res);

        exit;

    }
    /**
     * 产品运营分析:积分报表:清空表
     */
    protected function report1_1TRUNCATET(){
        $sql = "TRUNCATE TABLE `think_oms_report1`";
        $omsReport1M = M('omsReport1');
        $result = $omsReport1M->execute($sql);
    }
    /**
     * 产品运营分析:积分报表:计算可兑换天数
     */
    protected function report1_1CalculateAvailableDay(){
        $sql = "UPDATE `think_oms_report1` set available_day = available_stock/available_day";
        $omsReport1M = M('omsReport1');
        $result = $omsReport1M->execute($sql);
    }
    /**
     * 产品运营分析:积分报表:获取“所有”物料清单（报表数据库）
     */
    protected function getAllSKU(){

        $omsReport1M = M('omsReport1');
        $where = "";
        $group = "sku";
        $order = "sku";
        $filed = "sku";
        $res = $omsReport1M->where($where)->group($group)->order($order)->field($filed)->select();
        $allSkuStr = "";
        foreach ($res as $key => $item){
            $allSkuStr .= "'".$item['sku']."',";
        }
        $s = substr($allSkuStr, 0, $allSkuStr . count() - 1);
        $r_s = '('.$s.')';

        $this->allSKU = $r_s;
        return $r_s;

    }
    /**
     * 产品运营分析:备货参考表:OMS数据:可用库存
     */
    function report1_1CalculateOnHandOMS(){

        $t = $this->allSKU.count();
        if($t) {
        }else{
            $this->getAllSKU();
        }

        /*$report1SQL = "
        SELECT
sku_code AS sku,
SUM(available_stock) AS ontheway_stock
FROM oms_warehouse_sku_stock
WHERE
warehouse_code in ('A02','HYWCK','JDGD')
AND
sku_code in
        ".$this->allSKU." GROUP BY sku_code";*/

        import("@.ORG.Util.OmsDatabase");
        $omsDatabase = OmsDatabase::getInstance();
        $model = $omsDatabase->getModel();
        $where = "warehouse_code in ('A02','HYWCK','JDGD') AND sku_code in ".$this->allSKU;
        $group = "sku_code";
        $filed = "sku_code AS sku,SUM(available_stock) AS ontheway_stock";
        $data = $model->db(4)->table('oms_warehouse_sku_stock')->where($where)->group($group)->field($filed)->select();
        //DataGrip:.oms[73][2].

        $omsReport1M = M('omsReport1');
        foreach ($data as $key => $item) {
            $saveData = array(
                'ontheway_stock' => $item['ontheway_stock'],
            );
            $r = $omsReport1M->where("sku='{$item['sku']}' ")->save($saveData);
        }

    }
    /**
     * 产品运营分析:积分报表:连接SAP-SQLSERVER:在途
     */
    function report1_1CalculateOnHand(){

        $t = $this->allSKU.count();
        if($t) {
        }else{
            $this->getAllSKU();
        }

        $report1SQL = "
        SELECT
ItemCode AS sku,
SUM(Onorder) AS ontheway_stock
FROM OITW
WHERE
WhsCode in ('HYWCK','A02')
AND
ItemCode in
        ".$this->allSKU." GROUP BY ItemCode";
        $data = $this->fetchDataSAP($report1SQL);
        //DataGrip:.10[2][0].

        $omsReport1M = M('omsReport1');
        foreach ($data as $key => $item) {
            $saveData = array(
                'ontheway_stock' => $item['ontheway_stock'],
            );
            $r = $omsReport1M->where("sku='{$item['sku']}' ")->save($saveData);
        }

    }
    /**
     * 产品运营分析:积分报表:连接SAP-SQLSERVER:去年累计
     */
    protected function report1_1CalculateLastyearApply(){

        $t = $this->allSKU.count();
        if($t) {
        }else{
            $this->getAllSKU();
        }

        $report1SQL = "
SELECT
b.u_cardcode AS p_code,
a.U_ItemCode AS sku,
sum(U_Qty) AS qty
FROM [@ti_z0081] a JOIN [@ti_z0080] b
ON a.DocEntry=b.DocEntry
WHERE
  b.U_DocDate>='2017-01-01'
  AND b.U_DocDate<='2017-12-31'
  AND a.U_OStatus IN ('9','11')
  AND a.U_ItemCode IN
".$this->allSKU.
" GROUP BY b.u_cardcode,a.U_ItemCode
ORDER BY b.u_cardcode
";
        $data = $this->fetchDataSAP($report1SQL);
        //DataGrip:.10:47.

        $omsReport1M = M('omsReport1');
        foreach ($data as $key => $item) {
            $saveData = array(
                'lastyear_apply' => $item['qty'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }

    }
    /**
     * 产品运营分析:积分报表:连接SAP-SQLSERVER:今年累计SAP
     */
    function report1_1CalculateThisyearApplySAP(){

        $t = $this->allSKU.count();
        if($t) {
        }else{
            $this->getAllSKU();
        }

        $report1SQL = "
        SELECT
b.u_cardcode AS p_code,
a.U_ItemCode AS sku,
sum(U_Qty) as qty
FROM [@ti_z0081] a JOIN [@ti_z0080] b
ON a.DocEntry=b.DocEntry
WHERE b.U_DocDate>='2018-01-01'
AND b.U_DocDate<='2018-12-31'
AND a.U_OStatus IN ('9','11')
AND a.U_ItemCode IN
".$this->allSKU.
" GROUP BY b.u_cardcode,a.U_ItemCode
ORDER BY b.u_cardcode
";
        $data = $this->fetchDataSAP($report1SQL);
        //DataGrip:.10:48.

        $omsReport1M = M('omsReport1');
        foreach ($data as $key => $item) {
            $saveData = array(
                'sapthisyear_apply' => $item['qty'],
            );
            $r = $omsReport1M->where("p_code='{$item['p_code']}' and sku='{$item['sku']}' ")->save($saveData);
        }

    }
    /**
     * 产品运营分析:积分报表:今年累计
     */
    protected function report1_1CalculateThisyearApply(){
        $sql = "UPDATE `think_oms_report1` set thisyear_apply = sapthisyear_apply + thisyear_apply";
        $omsReport1M = M('omsReport1');
        $result = $omsReport1M->execute($sql);
    }
    // Attachment: 生成Excel拷贝
    protected function generateExcelReport1_1A1($filename,$normalData){

        Vendor('Excel.PHPExcel');

        $objPHPExcel = new \PHPExcel();
        $currentSheet = $objPHPExcel->getSheet(0);
        $m = 1;
        $currentSheet->setCellValue('A'.$m, '平台');
        $currentSheet->setCellValue('B'.$m, '物料编号');
        $currentSheet->setCellValue('C'.$m, '物料描述');
        $currentSheet->setCellValue('D'.$m, '可用库存');

        $currentSheet->setCellValue('E'.$m, '可兑换天数');

        $currentSheet->setCellValue('F'.$m, '在途库存');

        $currentSheet->setCellValue('G'.$m, '日均兑换量');
        $currentSheet->setCellValue('H'.$m, '上月兑换量');
        $currentSheet->setCellValue('I'.$m, '今年累计兑换量');
        $currentSheet->setCellValue('J'.$m, '去年累计兑换量');
        $currentSheet->setCellValue('K'.$m, '警戒库存');

        $currentSheet->setCellValue('L'.$m, '订购提醒');
        $currentSheet->setCellValue('M'.$m, '1');
        $currentSheet->setCellValue('N'.$m, '2');
        $currentSheet->setCellValue('O'.$m, '3');
        $currentSheet->setCellValue('P'.$m, '4');

        $currentSheet->setCellValue('Q'.$m, '5');
        $currentSheet->setCellValue('R'.$m, '6');
        $currentSheet->setCellValue('S'.$m, '7');
        $currentSheet->setCellValue('T'.$m, '8');
        $currentSheet->setCellValue('U'.$m, '9');

        $currentSheet->setCellValue('V'.$m, '10');
        $currentSheet->setCellValue('W'.$m, '11');
        $currentSheet->setCellValue('X'.$m, '12');
        $currentSheet->setCellValue('Y'.$m, '13');
        $currentSheet->setCellValue('Z'.$m, '14');

        $currentSheet->setCellValue('AA'.$m, '15');
        $currentSheet->setCellValue('AB'.$m, '16');
        $currentSheet->setCellValue('AC'.$m, '17');
        $currentSheet->setCellValue('AD'.$m, '18');
        $currentSheet->setCellValue('AE'.$m, '19');

        $currentSheet->setCellValue('AF'.$m, '20');
        $currentSheet->setCellValue('AG'.$m, '21');
        $currentSheet->setCellValue('AH'.$m, '22');
        $currentSheet->setCellValue('AI'.$m, '23');
        $currentSheet->setCellValue('AJ'.$m, '24');

        $currentSheet->setCellValue('AK'.$m, '25');
        $currentSheet->setCellValue('AL'.$m, '26');
        $currentSheet->setCellValue('AM'.$m, '27');
        $currentSheet->setCellValue('AN'.$m, '28');
        $currentSheet->setCellValue('AO'.$m, '29');

        $currentSheet->setCellValue('AP'.$m, '30');
        $currentSheet->setCellValue('AQ'.$m, '31');

        $cellIndex=2;
        // $normalData
        foreach ($normalData as $k => $v){

            $currentSheet->setCellValueExplicit('A'.$cellIndex,$v['p_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('B'.$cellIndex,$v['sku'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('C'.$cellIndex,$v['sku_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('D'.$cellIndex,$v['available_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('E'.$cellIndex,$v['available_day'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('F'.$cellIndex,$v['ontheway_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('G'.$cellIndex,$v['daily_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('H'.$cellIndex,$v['lastmonth_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('I'.$cellIndex,$v['thisyear_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('J'.$cellIndex,$v['lastyear_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('K'.$cellIndex,$v['w_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('L'.$cellIndex,$v['w_w'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('M'.$cellIndex,$v['day_01'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('N'.$cellIndex,$v['day_02'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('O'.$cellIndex,$v['day_03'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('P'.$cellIndex,$v['day_04'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('Q'.$cellIndex,$v['day_05'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('R'.$cellIndex,$v['day_06'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('S'.$cellIndex,$v['day_07'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('T'.$cellIndex,$v['day_08'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('U'.$cellIndex,$v['day_09'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('V'.$cellIndex,$v['day_10'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('W'.$cellIndex,$v['day_11'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('X'.$cellIndex,$v['day_12'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('Y'.$cellIndex,$v['day_13'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('Z'.$cellIndex,$v['day_14'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AA'.$cellIndex,$v['day_15'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AB'.$cellIndex,$v['day_16'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AC'.$cellIndex,$v['day_17'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AD'.$cellIndex,$v['day_18'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AE'.$cellIndex,$v['day_19'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AF'.$cellIndex,$v['day_20'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AG'.$cellIndex,$v['day_21'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AH'.$cellIndex,$v['day_22'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AI'.$cellIndex,$v['day_23'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AJ'.$cellIndex,$v['day_24'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AK'.$cellIndex,$v['day_25'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AL'.$cellIndex,$v['day_26'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AM'.$cellIndex,$v['day_27'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AN'.$cellIndex,$v['day_28'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AO'.$cellIndex,$v['day_29'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AP'.$cellIndex,$v['day_30'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AQ'.$cellIndex,$v['day_31'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $cellIndex++;

        }


        $timeTag = date('Ymd_Hi');
        $filename = mb_convert_encoding($filename,'gbk','utf-8');
        $name = $filename.'_'.$timeTag.'.xls';
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $file = 'Uploads/autoreport/'.date('Ymd');
        if(!file_exists($file)){
            mkdir($file);
        }

        $fullPath = $file.'/'.$name;
        $objWriter->save($fullPath);
        return $fullPath;

    }
    // Attachment: 生成Excel拷贝
    protected function generateExcelReport1_1A1_2($filename,$normalData){

        Vendor('Excel.PHPExcel');

        $objPHPExcel = new \PHPExcel();
        $currentSheet = $objPHPExcel->getSheet(0);
        $m = 1;
        $currentSheet->setCellValue('A'.$m, '平台');
        $currentSheet->setCellValue('B'.$m, '物料编号');

        // new items begin:
        $currentSheet->setCellValue('C'.$m, '品牌');
        $currentSheet->setCellValue('D'.$m, '产品经理');
        // new items end.

        $currentSheet->setCellValue('E'.$m, '物料描述');
        $currentSheet->setCellValue('F'.$m, '日均兑换量');
        $currentSheet->setCellValue('G'.$m, '上月兑换量');

        $currentSheet->setCellValue('H'.$m, '今年累计兑换量');
        $currentSheet->setCellValue('I'.$m, '去年累计兑换量');

        $currentSheet->setCellValue('J'.$m, '可用库存');

        $currentSheet->setCellValue('K'.$m, '1');
        $currentSheet->setCellValue('L'.$m, '2');
        $currentSheet->setCellValue('M'.$m, '3');
        $currentSheet->setCellValue('N'.$m, '4');

        $currentSheet->setCellValue('O'.$m, '5');
        $currentSheet->setCellValue('P'.$m, '6');
        $currentSheet->setCellValue('Q'.$m, '7');
        $currentSheet->setCellValue('R'.$m, '8');
        $currentSheet->setCellValue('S'.$m, '9');

        $currentSheet->setCellValue('T'.$m, '10');
        $currentSheet->setCellValue('U'.$m, '11');
        $currentSheet->setCellValue('V'.$m, '12');
        $currentSheet->setCellValue('W'.$m, '13');
        $currentSheet->setCellValue('X'.$m, '14');

        $currentSheet->setCellValue('Y'.$m, '15');
        $currentSheet->setCellValue('Z'.$m, '16');
        $currentSheet->setCellValue('AA'.$m, '17');
        $currentSheet->setCellValue('AB'.$m, '18');
        $currentSheet->setCellValue('AC'.$m, '19');

        $currentSheet->setCellValue('AD'.$m, '20');
        $currentSheet->setCellValue('AE'.$m, '21');
        $currentSheet->setCellValue('AF'.$m, '22');
        $currentSheet->setCellValue('AG'.$m, '23');
        $currentSheet->setCellValue('AH'.$m, '24');

        $currentSheet->setCellValue('AI'.$m, '25');
        $currentSheet->setCellValue('AJ'.$m, '26');
        $currentSheet->setCellValue('AK'.$m, '27');
        $currentSheet->setCellValue('AL'.$m, '28');
        $currentSheet->setCellValue('AM'.$m, '29');

        $currentSheet->setCellValue('AN'.$m, '30');
        $currentSheet->setCellValue('AO'.$m, '31');

        $cellIndex=2;
        // $normalData
        foreach ($normalData as $k => $v){

            $currentSheet->setCellValueExplicit('A'.$cellIndex,$v['p_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('B'.$cellIndex,$v['sku'], \PHPExcel_Cell_DataType::TYPE_STRING);

            // new items begin:
            $currentSheet->setCellValueExplicit('C'.$cellIndex,$v['brand_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('D'.$cellIndex,$v['product_manager'], \PHPExcel_Cell_DataType::TYPE_STRING);
            // new items end.

            $currentSheet->setCellValueExplicit('E'.$cellIndex,$v['sku_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('F'.$cellIndex,$v['daily_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('G'.$cellIndex,$v['lastmonth_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('H'.$cellIndex,$v['thisyear_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('I'.$cellIndex,$v['lastyear_apply'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('J'.$cellIndex,$v['ontheway_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('K'.$cellIndex,$v['day_01'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('L'.$cellIndex,$v['day_02'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('M'.$cellIndex,$v['day_03'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('N'.$cellIndex,$v['day_04'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('O'.$cellIndex,$v['day_05'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('P'.$cellIndex,$v['day_06'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('Q'.$cellIndex,$v['day_07'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('R'.$cellIndex,$v['day_08'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('S'.$cellIndex,$v['day_09'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('T'.$cellIndex,$v['day_10'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('U'.$cellIndex,$v['day_11'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('V'.$cellIndex,$v['day_12'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('W'.$cellIndex,$v['day_13'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('X'.$cellIndex,$v['day_14'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('Y'.$cellIndex,$v['day_15'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('Z'.$cellIndex,$v['day_16'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AA'.$cellIndex,$v['day_17'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AB'.$cellIndex,$v['day_18'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AC'.$cellIndex,$v['day_19'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AD'.$cellIndex,$v['day_20'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AE'.$cellIndex,$v['day_21'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AF'.$cellIndex,$v['day_22'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AG'.$cellIndex,$v['day_23'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AH'.$cellIndex,$v['day_24'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AI'.$cellIndex,$v['day_25'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AJ'.$cellIndex,$v['day_26'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AK'.$cellIndex,$v['day_27'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AL'.$cellIndex,$v['day_28'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AM'.$cellIndex,$v['day_29'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('AN'.$cellIndex,$v['day_30'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('AO'.$cellIndex,$v['day_31'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $cellIndex++;

        }


        $timeTag = date('Ymd_Hi');
        $filename = mb_convert_encoding($filename,'gbk','utf-8');
        $name = $filename.'_'.$timeTag.'.xls';
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $file = 'Uploads/autoreport/'.date('Ymd');
        if(!file_exists($file)){
            mkdir($file);
        }

        $fullPath = $file.'/'.$name;
        $objWriter->save($fullPath);
        return $fullPath;

    }
    /**
     * 产品运营分析:积分报表:发送邮件
     */
    function sendOutR1(){

        ini_set('memory_limit', '512M');

        $hour = date('H');
        if($hour > 8 && $hour <23){}
        else{
            die();
        }

        $to = array(
            //'ibd-all@g-town.com.cn',
            //'DDGLZ@G-TOWN.COM.CN',
            //'mario@g-town.com.cn',
            //'eddy.chen@g-town.com.cn',
            'Shermie.Xu@g-town.com.cn',
            'GT.ALL.JF@g-town.com.cn',
            'Mandy.Yan@g-town.com.cn',
            //'antoine.wei@g-town.com.cn',
            'Sophie.Li@g-town.com.cn',
            //'maxwell.shi@g-town.com.cn',
            'molly.li@g-town.com.cn',
            'emily.li@g-town.com.cn',
            //'yunwen.xu@g-town.com.cn',
            'Tony@g-town.com.cn',
            'Wallace.Ding@g-town.com.cn',
            'Fangfang.Shi@g-town.com.cn'
            //'Andy.Fang@g-town.com.cn'
        );
        $cc = array(
        );

        $attachment = array();

        $omsReport1M = M('omsReport1');
        $data = $omsReport1M->select();
        $excelfile1 = $this->generateExcelReport1_1A1_2('备货参考表',$data);
        $attachment[] = $excelfile1;
        $mailConnent = '';

        $currDate = date('Y-m-d');

        $this->sendReport($to, "备货参考表-".$currDate, $mailConnent,$attachment,$cc);
        exit();

    }

    /**
     * 备货分析
     */
    function report1_2(){

        ini_set('memory_limit', '512M');

        $before_0 = date('Y-m-d');
        $before_1 = date("Y-m-d", strtotime("-1 day"));
        $before_2 = date("Y-m-d", strtotime("-2 day"));
        $before_3 = date("Y-m-d", strtotime("-3 day"));
        $before_7 = date("Y-m-d", strtotime("-7 day"));
        $before_15 = date("Y-m-d", strtotime("-15 day"));
        $before_30 = date("Y-m-d", strtotime("-30 day"));
        $before_60 = date("Y-m-d", strtotime("-60 day"));

        import("@.ORG.Util.OmsDatabase");
        $omsDatabase = OmsDatabase::getInstance();
        $model = $omsDatabase->getModel();

        $filed = ("

  b.sku_code AS 'sku',
  b.product_name AS 'sku_name',
  c.brand_name AS 'brand_name',
  d.user_name AS 'pm_name',
sum(oms_warehouse_sku_stock.available_stock) AS 'tmp_available_stock',
sum(oms_warehouse_sku_stock.onorder) AS 'ontheway_stock'

        ");

        $join = "
        left join oms_product_sku AS b
    on oms_warehouse_sku_stock.sku_code = b.sku_code
left join oms_product_spu AS c
    on b.spu_code = c.spu_code
left join sys_user AS d
    on c.product_manager_id = d.id
    ";

        $where="
        
        1=1
  and oms_warehouse_sku_stock.available_stock != 0
  and oms_warehouse_sku_stock.sku_code != ''
  and b.sku_code != ''
  and oms_warehouse_sku_stock.warehouse_code in ('H03','HYWCK')
  
        ";

        $group = "oms_warehouse_sku_stock.sku_code";

        $order = "oms_warehouse_sku_stock.sku_code desc";

        //Datagrip:5
        /*$res = $model->db(4)->table('oms_warehouse_sku_stock')->join($join)->where($where)->group($group)->order($order)->field($filed)->select();
        $omsReport1M = M('omsReport2');
        foreach ($res as $key => $item) {
            $saveData = array(
                'sku' => $item['sku'],
                'sku_name' => $item['sku_name'],
                'brand_name' => $item['brand_name'],
                'pm_name' => $item['pm_name'],
                'available_stock' => $item['tmp_available_stock'],
                'ontheway_stock' => $item['ontheway_stock'],
            );
            $r = $omsReport1M->add($saveData);
        }*/

        //计算可用库存:available_stock(时间范围：-)
        $filed = ("
        sku_code AS sku,
    sum(num) AS t_apply
        ");

        $where="
        
        1=1
  and order_type = 1
  and order_status in (220, 0)
  
        ";

        $group = "sku_code";

        $order = "sku_code desc";

        //Datagrip:6

        /*$res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        $omsReport1M = M('omsReport2');
        foreach ($res as $key => $item) {
            // 查出原值
            $oldInfo = $omsReport1M->where("sku='".$item['sku']."'")->find();
            if(is_array($oldInfo)){
                // 更新数据
                $saveData = array(
                    'available_stock' => $oldInfo['available_stock']-$item['t_apply'],
                );
                $r = $omsReport1M->where("sku='{$item['sku']}'")->save($saveData);
            }
        }*/

        //计算销量:8个(时间范围：60天)
        $filed = ("
        sku_code AS sku,
    sum(num) AS t_apply
        ");

        $where="
        
        1=1
  and order_type = 1
  and order_status in (220, 0)
  and create_time >= '".$before_60." ' and oms_order_detail.create_time <= '".$before_0."'
  
        ";

        $group = "sku_code";

        $order = "sku_code desc";

        //Datagrip:7

        $res = $model->db(4)->table('oms_order_detail')->where($where)->group($group)->order($order)->field($filed)->select();
        $omsReport1M = M('omsReport2');
        $i=0;
        foreach ($res as $key => $item) {
            // 更新数据
            $saveData = array(
                'before_60' => $item['t_apply'],
            );
            $r = $omsReport1M->where("sku='{$item['sku']}'")->save($saveData);
            $i++;
        }
        echo $i;


    }
    function sendOutR2(){

        ini_set('memory_limit', '512M');

        $hour = date('H');
        if($hour > 8 && $hour <23){}
        else{
            die();
        }

        $to = array(
            //'maxwell.shi@g-town.com.cn',
            'eddy.chen@g-town.com.cn',
            'molly.li@g-town.com.cn',
            'yunwen.xu@g-town.com.cn',
        );
        $cc = array(
        );

        $attachment = array();

        $omsReport1M = M('omsReport2');
        $data = $omsReport1M->select();
        $excelfile1 = $this->generateExcelReport1_2A1('备货报表',$data);
        $attachment[] = $excelfile1;
        $mailConnent = '';

        $this->sendReport($to, "备货报表(测试)：OMS数据-1031", $mailConnent,$attachment,$cc);
        exit();

    }
    // Attachment: 生成Excel拷贝
    protected function generateExcelReport1_2A1($filename,$normalData){

        Vendor('Excel.PHPExcel');

        $objPHPExcel = new \PHPExcel();
        $currentSheet = $objPHPExcel->getSheet(0);
        $m = 1;
        $currentSheet->setCellValue('A'.$m, '物料号');
        $currentSheet->setCellValue('B'.$m, '物料名');
        $currentSheet->setCellValue('C'.$m, '品牌');
        $currentSheet->setCellValue('D'.$m, '产品经理');
        $currentSheet->setCellValue('E'.$m, 'BU总监');

        $currentSheet->setCellValue('F'.$m, '可用库存');
        $currentSheet->setCellValue('G'.$m, '在途库存');
        $currentSheet->setCellValue('H'.$m, '今日销量');
        $currentSheet->setCellValue('I'.$m, '前1天销量');
        $currentSheet->setCellValue('J'.$m, '前2天销量');

        $currentSheet->setCellValue('K'.$m, '前3天销量');
        $currentSheet->setCellValue('L'.$m, '前7天销量');
        $currentSheet->setCellValue('M'.$m, '前15天销量');
        $currentSheet->setCellValue('N'.$m, '前30天销量');
        $currentSheet->setCellValue('O'.$m, '前60天销量');

        $cellIndex=2;
        // $normalData
        foreach ($normalData as $k => $v){

            $currentSheet->setCellValueExplicit('A'.$cellIndex,$v['sku'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('B'.$cellIndex,$v['sku_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('C'.$cellIndex,$v['brand_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('D'.$cellIndex,$v['pm_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
            $currentSheet->setCellValueExplicit('E'.$cellIndex,$v['h_pm_name'], \PHPExcel_Cell_DataType::TYPE_STRING);

            $currentSheet->setCellValueExplicit('F'.$cellIndex,$v['available_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('G'.$cellIndex,$v['ontheway_stock'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('H'.$cellIndex,$v['before_0'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('I'.$cellIndex,$v['before_1'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('J'.$cellIndex,$v['before_2'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $currentSheet->setCellValueExplicit('K'.$cellIndex,$v['before_3'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('L'.$cellIndex,$v['before_7'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('M'.$cellIndex,$v['before_15'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('N'.$cellIndex,$v['before_30'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);
            $currentSheet->setCellValueExplicit('O'.$cellIndex,$v['before_60'], \PHPExcel_Cell_DataType::TYPE_NUMERIC);

            $cellIndex++;

        }


        $timeTag = date('Ymd_Hi');
        $filename = mb_convert_encoding($filename,'gbk','utf-8');
        $name = $filename.'_'.$timeTag.'.xls';
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $file = 'Uploads/autoreport/'.date('Ymd');
        if(!file_exists($file)){
            mkdir($file);
        }

        $fullPath = $file.'/'.$name;
        $objWriter->save($fullPath);
        return $fullPath;

    }

    // 通用开始
    // Template
    function template(){

        ini_set('memory_limit', '512M');

        $currDate = date('Y-m-d');
        $firstDay = date('Y-m-1');
        $howmanyDays = date("d");

        $lastMonthFirstDay = date("Y-m-1", strtotime("-1 month"));
        $currMonthFristDay = date('Y-m-1');
        $nextMonthFirstDay = date("Y-m-1", strtotime("+1 month"));
        $lastYearFirstDay = date("Y-1-1", strtotime("-1 year"));
        $lastYearEndDay = date("Y-12-31", strtotime("-1 year"));
        $currYearFirstDay = date("Y-1-1");
        $currYearEndDay = date("Y-12-31");

        import("@.ORG.Util.OmsDatabase");
        $omsDatabase = OmsDatabase::getInstance();
        $model = $omsDatabase->getModel();

        $filed = ("
       
        ");

        $join = "";

        $where="
        
        ";

        $group = "";

        $order = "";

        //Datagrip:2
        $res = $model->db(4)->table('oms_order_detail')->join($join)->where($where)->group($group)->order($order)->field($filed)->select();

        // save to database:
        $omsReport1M = M('omsReport1');
        foreach ($res as $key => $item) {
            $saveData = array(
                'p_code' => $item['p_code'],
                'p_name' => $item['p_name'],
                'sku' => $item['sku'],
                'sku_name' => $item['sku_name'],
                'available_stock' => $item['true_available_stock'],
                'ontheway_stock' => $item['ontheway_stock'],
                'daily_apply' => $item['daily_apply'],
                'w_stock' => $item['w_stock'],
                'w_w' => $item['w_w'],
            );
            $r = $omsReport1M->add($saveData);
        }

    }
    protected  function sendReport($tomail,$title,$connent,$attachment=array(),$cc=array()){
        //邮件模版    内容替换%connent%
        $mailmodel =<<<html
<div>%connent%</div>
html;
        $connent = str_replace("%connent%", $connent ,$mailmodel);
        return $this->sendEamilAutoReport($tomail, $title, $connent,$attachment,$cc);
    }
    // Send Email
    protected function sendEamilAutoReport($toMail, $title, $content, $Attachment,$cc=array())
    {
        $respon = array(
            'code' => 0,
            'msg' => ''
        );


        import("@.ORG.Util.send");
        $mail = new \PHPMailer(true);

        $mail->CharSet = "UTF-8";
        $mail->IsSMTP();
        // 这里指定字符集！解决中文乱码问题
        $mail -> Encoding = "base64";
        $mail->SMTPAuth = true;
        //$mail->SMTPSecure = "ssl";
        $mail->Host = "mail.g-town.com.cn";
        $mail->Port = 587;
        $mail->Username = 'oa@g-town.com.cn';
        $mail->Password ='Public321';
        $mail->SetFrom($mail->Username, '海牙湾');
        //$mail->AddReplyTo($mail->Username , '测试');
        //if(session('emp_no'))
        //$mail->AddReplyTo(session('emp_no').'@g-town.com.cn',session('emp_no'));
        // 设置邮件回复人地址和名称
        $mail->Subject = '【'.$title.' 】'; // 设置邮件标题
        $mail->AltBody = "为了查看该邮件，请切换到支持 HTML 的邮件客户端";

        // 可选项，向下兼容考虑
        $mail->MsgHTML($content); // 设置邮件内容

        if($toMail) {
            foreach ($toMail as $to) {
                $mail->AddAddress($to);
            }
        }

        if($cc) {
            foreach ($cc as $c) {
                $mail->AddCC($c);
            }
        }

        if ($Attachment){
            foreach ($Attachment as $a) {
                //$trueName = end(explode("/",$a));
                //var_dump($trueName);die();
                $mail->AddAttachment($a); // 附件
            }
        }

        //$ats = $mail->GetAttachments();
        //var_dump($ats);die();

        //exit();

        if (! $mail->Send()) {

            $respon['code'] = - 1;
            $respon['msg'] = $mail->ErrorInfo;
        } else {
            $respon['msg'] = "邮件发送成功！";
        }

        return $respon;
    }

    function http($url, $params, $method = 'GET', $header = array(), $multi = false) {
        $opts = array(CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header);

        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)) {
            case 'GET' :
                $opts[CURLOPT_URL] = $url . '?' . str_replace("&amp;", "&", http_build_query($params));
                break;
            case 'POST' :
                //判断是否传输文件
                //$params = $multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            default :
                throw new Exception('不支持的请求方式！');
        }

        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error)
            throw new Exception('请求发生错误：' . $error);
        return $data;
    }

    function requestService($url, $data = null, $simaple = false,$is_json=false,$header = array())
    {

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if($header){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        if (!empty($data)&&$is_json==false){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        }else{
            if($data){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data,JSON_UNESCAPED_UNICODE));
            }
        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        $error = curl_error($curl);
        //echo $curl;
        curl_close($curl);

        if($simaple) {
            $response = array('status' => 0, 'error' => '', 'code' => '0', 'msg' => '', $data = array());
            if($httpCode == 200) {
                $result = json_decode($output, true);
                if($result) {
                    $response['status'] = 1;
                    $response['code'] = $result['code'];
                    $response['msg'] = $result['msg'];
                    $response['data'] = $result['data'];
                } else {
                    $response['error'] = $output;
                }
            } else {
                $response['error'] = $error;
            }
            return $response;
        } else {
            return array(
                'httpCode' => $httpCode,
                'error' => $error,
                'output' => $output,
                'data' => json_decode($output, true),
            );
        }
    }

    // Fetch DataSAP
    protected function fetchDataSAP($sqlquery){
        import("@.ORG.Util.Autoreportsqlserver");
        $sqlserver  = new Autoreportsqlserver();
        $data = $sqlserver->query($sqlquery);
        $sqlserver->close();
        return $data;
    }
    // 通用结束

    function xxx(){

        $command = "e: & cd E:\DesktopData\Projects\Java\cli & java -jar cli002.jar \"{code:0,data:[{date:1,sales:99}]}\"" ;
        exec($command,$output);
        var_dump($output);

    }

}